package main

import (
	"fmt"
	"sync"
	"sync/atomic"
	"time"
)

const TOTAL = 10000

var wg sync.WaitGroup
/*
原子操作
https://www.cnblogs.com/gpan/p/9849433.html
 */
func main() {
	fmt.Println("hello atomic")
	var num, num1 int32

	for i := 0; i < TOTAL; i++ {
		go func(num *int32) {
			wg.Add(1)
			AddNumWithAtomic(num)
			wg.Done()
		}(&num)
		go func(num1 *int32) {
			wg.Add(1)
			AddNumD(num1)
			wg.Done()
		}(&num1)

	}

	time.Sleep(time.Second * 2)

	fmt.Println("use AddNumWithAtomic", num)
	fmt.Println("use AddNumD", num1)

	wg.Wait()

}

func AddNumWithAtomic(num *int32) {
	atomic.AddInt32(num, 1)
}

func AddNumD(num *int32) {
	*num++
}
